'use strict';

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var orderList = [],
    pageNum = 1,
    defultSize = 10,
    commentsHtml = void 0,
    orderPage = 1;
//课程评价
function mycomments() {
  var obj = [{ img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '不得不说,老师声音非常的有魔性,讲的也清晰易懂,我在某机构报名的课程三个月才讲完基本的操作,而且大部分都是一些废话,也不深入,如果此套课程收费话 我也愿意购买,我从来没有听一遍就能听的这么清晰明白的，还是没遇到好老师啊 嘿嘿', time: '2020-03-05', id: '344' }, { img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '我在某机构报名的课程三个月才讲完基本的操作,', time: '2020-03-05', id: '344' }, { img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '我在某机构报名的课程三个月才讲完基本的操作,,讲的也清晰易懂', time: '2020-03-05', id: '344' }, { img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '不得不说,老师声音非常的有魔性,讲的也清晰易懂,我在某机构报名的课程三个月才讲完基本的操作,而且大部分都是一些废话,也不深入,如果此套课程收费话 我也愿意购买,我从来没有听一遍就能听的这么清晰明白的，还是没遇到好老师啊 嘿嘿', time: '2020-03-05', id: '344' }, { img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '我在某机构报名的课程三个月才讲完基本的操作,', time: '2020-03-05', id: '344' }, { img: 'images/class_2.png', name: '《保险微营销基础课程》网络营销 微信营销 电子商务', content: '我在某机构报名的课程三个月才讲完基本的操作,,讲的也清晰易懂', time: '2020-03-05', id: '344' }];
  var html = '',
      length = obj.length > 10 ? 10 : obj.length;

  for (var i = 0; i < length; i++) {
    html += ' <div class="comments-box">\n      <div class="content-box">\n        <img src=' + obj[i].img + ' style="width: 282px;height: 154px;" class="img-responsive">\n        <div class="right-box">\n          <div class="title">' + obj[i].name + '</div>\n          <div class="comments-content">' + obj[i].content + '</div>\n          <div class="comments-content">\u65F6\u95F4\uFF1A' + obj[i].time + '</div>\n        </div>\n      </div>\n    </div>';
  }
  $("#mycomments").html(html);
}

// 我的评价
function curriculumCommentsContent() {
  var userInfo = {};
  try {
    userInfo = JSON.parse(sessionStorage.hz_userInfo);
  } catch (err) {
    userInfo = {};
  }
  $.server({ pageSize: defultSize, pageNum: pageNum }, '/iagent/v1/queryUserCommentList').then(function (res) {
    var obj = res.result.data;
    var html = '';
    for (var i = 0; i < obj.length; i++) {
      html += ' <div class="comments-box">\n      <div class="content-box comments-content-box-reset">\n        <img src=' + (userInfo.avatar || "images/defultHeader.png") + '  style="width: 48px;height: 48px;border-radius: 50%;" class="img-responsive">\n        <div class="right-box">\n          <div class="comments-content">' + obj[i].createTime + '</div>\n          <div class="comments-content">' + obj[i].comment + '</div>\n          <div class="lessonInfoBox flexLay">\n            <img src=' + obj[i].poster + ' />\n            <div>' + obj[i].title + '</div>\n          </div>\n        </div>\n      </div>\n    </div>';
    }
    if (res.result.total == 0) {
      $("#curriculumCommentsContent").html("您暂无评论信息");
      return;
    }
    if (res.result.totalPage <= pageNum) {
      $(".viewMore").fadeOut();
      $(".nocontent").fadeIn();
    } else {
      $(".viewMore").fadeIn();
      $(".nocontent").fadeOut();
    }
    $("#curriculumCommentsContent").append(html);
  });
}
//查看更多 评论
function viewMoreComment() {
  pageNum++;
  this.curriculumCommentsContent();
}

function getFavoriteList() {
  $.server({ pageSize: 100, pageNum: 1 }, '/iagent/v1/queryUserFavoriteList').then(function (res) {
    collection(res.result.data);
  });
}

// 我的收藏
function collection(obj) {

  var html = '',
      length = obj.length > 10 ? 10 : obj.length;
  for (var i = 0; i < length; i++) {
    html += ' <div class="comments-box">\n    <div class="content-box head-content-box">\n      <img src=' + obj[i].poster + ' style="width: 282px;height: 154px;" class="img-responsive">\n      <div class="right-box curriculum-box">\n        <div class="title" onclick="goLessonDetail(' + obj[i].bizId + ',' + obj[i].bizType + ')">\n          ' + obj[i].title + ' <span class="lessonType">' + (obj[i].bizType == 1 ? "资讯" : '课程') + '\n        </div>\n        <div class="comments-content curriculum"></div>\n        <div class="comments-content curriculum flexLay curriculum-last">\n          <span class="sum">' + obj[i].createTime + '</span>\n          <div class="appointmentbtn" onclick="cancelCollection(' + obj[i].bizId + ',' + obj[i].bizType + ')">\u53D6\u6D88\u6536\u85CF</div>\n        </div>\n      </div>\n    </div>\n  </div>';
  }
  $("#collection").html(html);
}
//取消收藏
function cancelCollection(id, type) {
  var r = confirm("确定取消此项收藏?");
  if (r == true) {
    var params = {
      bizType: type,
      bizId: id
    };
    $.server(params, '/iagent/v1/delFavorite').then(function (res) {
      toastr.info('取消收藏成功');
      getFavoriteList();
    });
  }
  return;
}

// 我的订单
function order() {
  $.server({ "pageSize": 10, "pageNum": orderPage }, '/iagent/v1/userOrderList').then(function (res) {
    var obj = res.result.data;
    var html = '';
    if (orderPage == 1) {
      orderList = obj;
    } else {
      var _orderList;

      (_orderList = orderList).push.apply(_orderList, _toConsumableArray(obj));
    }
    for (var i = 0; i < orderList.length; i++) {
      var priceHtml = '';
      if (obj[i].priceStr != 0) {
        priceHtml = '<span class="money">\uFFE5' + obj[i].priceStr + '</span>';
      } else {
        priceHtml = '<span class="money">\u514D\u8D39</span>';
      }
      html += ' <div class="comments-box">\n      <div class="content-box head-content-box">\n        <img src=' + obj[i].poster + ' style="width: 282px;height: 154px;" class="img-responsive">\n        <div class="right-box curriculum-box">\n          <div class="title title-reset">' + obj[i].courseName + '</div>\n          <div class="comments-content curriculum">\u5B9E\u4ED8\u6B3E: ' + priceHtml + '</div>\n          <div class="comments-content curriculum">\u8BA2\u5355\u53F7: <span class="orderNumber">' + obj[i].ordreId + '</span></div>\n        </div>\n      </div>\n      <div class="evaluateContent">\n          <div class="starBox flexLay">\n            <span>\u6EE1\u610F\u5EA6:</span>\n            <div class="starImgBox" >\n              <img src="images/train/sc_active.png"  index="' + i + '" data="1"/>\n              <img src="images/train/sc_active.png"   index="' + i + '" data="2"/>\n              <img src="images/train/sc_active.png"  index="' + i + '" data="3"/>\n              <img src="images/train/sc_active.png"   index="' + i + '" data="4"/>\n              <img src="images/train/sc_active.png"   index="' + i + '" data="5"/>\n            </div>\n          </div>\n          <div class="evaluateBox">\n            <input class="evaluate"  placeholder="\u8BF7\u8F93\u5165\u8BC4\u4EF7\u5185\u5BB9"/>\n            <div class="evaluateBtn"   index="' + i + '"  data="' + obj[i].ordreId + '">\u786E\u8BA4\u8BC4\u4EF7</div>\n          </div>\n        </div>\n      </div>';
    }
    $("#order").html(html);
    if (res.result.totalPage <= page) {
      $(".viewMoreOrder").hide();
      $(".noOrderContent").show();
    } else {
      $(".viewMoreOrder").show();
      $(".noOrderContent").hide();
    }

    //满意度
    $(".starImgBox img").click(function () {
      var index = $(this).attr("data");
      var l = $(this).attr("index");
      var domARR = $(this).parent().find("img");
      for (var _i = 0; _i < domARR.length; _i++) {
        if ($(this).parent().find("img").eq(_i).attr("data") <= index) {
          $(this).parent().find("img").eq(_i).attr("src", "images/train/sc_active.png");
        } else {
          $(this).parent().find("img").eq(_i).attr("src", "images/train/xingx.png");
        }
      }
      orderList[l].score = index;
    });

    //评价调用接口
    $(".evaluateBtn").click(function () {
      var _this = this;

      var val = $(this).prev().val();
      var index = $(this).attr("data");
      var l = $(this).attr("index");
      if (!val) {
        return toastr.info("请输入评价内容");
      } else {
        console.log(val);
        $.server({ "bizType": 2, "bizId": orderList[l].courseId, content: val, star: orderList[l].score }, '/iagent/v1/addComment').then(function (res) {
          toastr.info('评论成功');
          $(_this).prev().val("");
          // order();
        });
      }
    });
  });
}
function viewMoreOrder() {
  orderPage++;
  order();
}

function appointment() {
  //我的预约
  $.server({}, '/iagent/v1/queryUserAppointment').then(function (res) {
    console.log(res, "用户预约列表");
    var obj1 = res.result.data;
    var html1 = '',
        length1 = obj1.length > 10 ? 10 : obj1.length; //最多显示多少条
    for (var i = 0; i < length1; i++) {
      html1 += '<div class="lessonItem flexLay">\n              <div class="lessonLeft" onclick="goLessonDetail(' + obj1[i].courseId + ',2)">\n                  <img src="' + obj1[i].poster + '" alt="">\n                  <div class="lessonInfo">\n                      <div class="lessonTit">\n                        ' + obj1[i].courseName + ' <span class="lessonType">\u76F4\u64AD\n                      </div>\n                      <div class="lessonTecher">\n                          <span>\u6388\u8BFE\u8001\u5E08:</span>\n                          <span>' + obj1[i].lecturerName + '</span>\n                      </div>\n                      <div class="lessonTecher">\n                          <span>\u5F00\u59CB\u65F6\u95F4:</span>\n                          <span>' + obj1[i].liveTime + '</span>\n                      </div>\n                  </div>\n              </div>\n              <div class="lessonRight comflex">\n                  <div class="appointmentbtn"   onclick="cancelLesson(' + obj1[i].courseId + ')">\u53D6\u6D88\u9884\u7EA6</div>\n              </div>\n          </div>';
    }
    $("#appointment").html(html1);
  });
}
// 取消预约
function cancelLesson(id) {
  $.server({ courseId: id }, '/iagent/v1/cancelBookCourse').then(function (res) {
    console.log(res, "用户取消预约");
    appointment();
    return toastr.info('取消成功');
  });
}
// function appointment(){
//   //我的预约
//   const obj1 = [{ title: '微商营销引流', typeName:'直播', auth: '杨蕾',id: '344',image: 'images/class_1.png',headerImg:'images/014758mlw1j471jvh47ng7.png',timer:'2020-07-25  21:30',},
//   { title: '人身保险分类', typeName:'专家课程',auth: '陈巍', id: '344', image: 'images/class_2.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-07-11  20:10', },
//   { title: '营销免费公开课',typeName:'直播', auth: '李扬', id: '344', image: 'images/class_4.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-06-21  04:03', },
//   { title: '投保指数祥解',typeName:'直播', auth: '鄂维南', id: '344', image: 'images/class_7.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-06-09  13:30', },
//   { title: '收入与投保的利害关系',typeName:'专家课程', auth: '丁成', id: '344', image: 'images/class_5.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-06-02  11:30', },
//   { title: '从收入指数与从业指数看行情',typeName:'专家课程', auth: '薛文', id: '344', image: 'images/class_6.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-05-23  12:30', },
//   { title: '财政你要知道的几项基础知识', typeName:'专家课程',auth: '杜成河', id: '344', image: 'images/class_2.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-05-18  16:30', },
//   { title: '创新变革中的行业危机', typeName:'直播',auth: '吴天', id: '344', image: 'images/class_3.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-05-12  15:16', },
//   { title: '中小企业的财政生命线', typeName:'专家课程',auth: '张成斌', id: '344', image: 'images/class_4.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-04-18  09:45', },
//   { title: '如何长远的看待保险指数变化',typeName:'专家课程', auth: '陈巍', id: '344', image: 'images/class_2.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2020-03-12  08:46', },
//   { title: '保险理赔你不知道的那些事',typeName:'专家课程', auth: '田小霞', id: '344', image: 'images/class_1.png', headerImg: 'images/014758mlw1j471jvh47ng7.png', timer: '2019-12-11  01:08', },];
// let html1 = '', length1 = obj1.length > 10 ? 10 : obj1.length;//最多显示多少条
// for (let i = 0; i < length1; i++) {
//   html1 += `<div class="lessonItem flexLay">
//               <div class="lessonLeft" onclick="goLessonDetail(${obj1[i].id},${obj1[i].type})">
//                   <img src="${obj1[i].image}" alt="">
//                   <div class="lessonInfo">
//                       <div class="lessonTit">
//                         ${obj1[i].title} <span class="lessonType">${obj1[i].typeName}
//                       </div>
//                       <div class="lessonTecher">
//                           <span>授课老师:</span>
//                           <span>${obj1[i].auth}</span>
//                       </div>
//                       <div class="lessonTecher">
//                           <span>开始时间:</span>
//                           <span>${obj1[i].timer}</span>
//                       </div>
//                   </div>
//               </div>
//               <div class="lessonRight comflex">
//                   <div class="appointmentbtn"   onclick="cancelLesson(${obj1[i].id})">取消预约</div>
//               </div>
//           </div>`;
//   }
//   $("#appointment").html(html1);
// }

/**
 * 查看课程详情
 */
function goLessonDetail(id, type) {
  if (type == 2) {
    location.href = 'trainDetail.html?id=' + id;
  } else {
    location.href = 'industryDetail.html?id=' + id + '&word="\u7528\u6237\u4E2D\u5FC3"&childTitle="\u6211\u7684\u6536\u85CF"';
  }
}

// 我的关注
// 专家列表数据
function expertArr() {
  if (validLogin()) {
    $.server({ pageSize: 10000, pageNum: 1 }, '/iagent/v1/queryUserFollowList').then(function (res) {
      console.log(res, "用户关注专家列表");
      var expertArrList = res.result.data;
      expert(expertArrList);
    });
  }
}
function expert(obj) {
  var html = '',
      length = obj.length;

  for (var i = 0; i < length; i++) {
    html += ' <div class="expertContent-box topic-box expert-reset title-box follow-reset col-md-12 col-sm-12 col-xs-12">\n    <div class="follow-box" onclick="expertDetail(' + obj[i].lecturerId + ')">\n     <img src=' + obj[i].avatar + '>\n      <div class="word-box">\n          <div class="name-box"><span class="name">' + obj[i].lecturerName + '/</span><span class="name-r">' + obj[i].title + '</span></div>\n          <div class="description">' + obj[i].introduction + '</div>\n      </div>\n      </div>\n      <div class="appointmentbtn" style="margin-right:10px" onclick=\'followN(' + obj[i].lecturerId + ')\'>\u53D6\u6D88\u5173\u6CE8</div>\n  </div>';
  }
  if (!html) {
    $("#expertContent").html("<div class='tips'>您暂未关注任何专家,清先关注再来查看吧</div>");
    return;
  }
  $("#expertContent").html(html);
}
// 跳转专家详情
function expertDetail(id) {
  location.href = 'expertDetail.html?id=' + id;
}

// 取消关注
function followN(id) {
  if (validLogin()) {
    var r = confirm("确定取消关注");
    if (r == true) {
      $.server({ followId: id }, '/iagent/v1/cancelFollow').then(function (res) {
        toastr.info('取消关注成功');
        expertArr();
      });
    }
  }
}
function loadQuestionList(){
  $.server({ pageSize: 100, pageNum: 1 }, '/iagent/v1/agentQuestions').then(function (res) {
    var obj = res.result;
    var html = '';
    for (var i = 0; i < obj.length; i++) {
      html += ' <div class="comments-box">'+     
      '<div class="content-box comments-content-box-reset">\n     '+     
      '<img src=' + (obj.userAvatar || "images/defultHeader.png") + '  style="width: 48px;height: 48px;border-radius: 50%;" class="img-responsive">\n   '+    
         '<div class="right-box">\n       '+  
            '<div class="comments-content">' + obj[i].addTime + '</div>\n      '+  
                '<div class="comments-content">' + obj[i].content + '</div>\n   '+       
                  '<div class="lessonInfoBox flexLay">\n         '+  
                     '<img src=' + obj[i].lecturerAvatar + ' />\n        '  
                     
            if(obj[i].answerList){
              for(var j=0; j<obj[i].answerList.length;j++){
                html +='<div>' + obj[i].answerList[j] + '</div>'
              } 

            } else{
              html+= "<div>该专家比较忙,暂无答复，请耐心等候喔~</div>"
            }
            html+=  '</div>\n '+ 
            '</div>\n      </div>\n    </div>';
    } 
    $("#myQuestionBox").append(html);
  });
}
/**
 * 页面初始化时加载 window.onload
 */
$(function () {
  //调用server加载页面数据
  mycomments();
  curriculumCommentsContent();
  order();
  appointment();
  expertArr();
  loadQuestionList();

  $("#topicContent .topic-title").click(function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(e);
    if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
  });

  $("#researchFindings .topic-title").click(function (e) {
    var index = e.currentTarget.dataset.index;
    if (index == 1) {} else if (index == 2) {} else if (index == 3) {}
  });

  // 修改信息
  $("#change-btn").click(function () {
    $("#model-box").show();
  });

  // 更换头像
  var peopel;
  $("#change-title-btn").change(function () {
    //avatar_file  input[file]的ID
    // 获取上传文件对象
    var file = $(this)[0].files[0];

    // 读取文件URL
    var reader = new FileReader();
    reader.readAsDataURL(file);
    // 阅读文件完成后触发的事件
    reader.onload = function () {
      // 读取的URL结果：this.result
      console.log(this.result, 8989);
      peopel = this.result;
      $("#userHeaderModel").attr("src", this.result); //avatar_img  img标签的ID
    };
  });
  //修改信息确定
  $("#change-sure").click(function () {
    var name = $("#changeUser").val();
    var params = void 0;
    if (!name) {
      return toastr.info('用户名不能为空！');
    }
    if (!peopel || !name) {
      params = peopel ? { avatar: peopel } : { name: name };
    } else {
      params = { name: name, avatar: peopel };
    }
    console.log(params, 777);
    $.server(params, '/iagent/v1/updateAvatarAndName').then(function (res) {
      console.log(res, "修改图像");
      toastr.info('修改成功');
      localStorage.hz_userInfo = JSON.stringify(res.result);
      sessionStorage.hz_userInfo = JSON.stringify(res.result);
      // window.location.reload()
      getUserInfo();
      $("#model-box").hide();
    });
  });
  //修改信息取消
  $("#change-no").click(function () {
    $("#model-box").hide();
  });
});